package leetCode;

public class ErFen {

    public static void main(String[] args) {
        int[] nums = {-5, 0, 1, 2, 3, 5, 7, 9, 10, 15, 20, 28, 50, 56};
        System.out.println(commonBinarySearch(nums, 56, 0, nums.length - 1));
    }

    public static Integer commonBinarySearch(int[] arr, int target, int left, int right) {

        if (arr == null) {
            return -1;
        }

        //边界条件
        if (target > arr[right] || target < arr[left] || left > right) return -1;
        if (left == right && arr[left] != target) return -1;
        if (arr[left] == target) return left;
        if (arr[right] == target) return right;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] == target) return mid;

            if (right == left + 1) return -1;

            if (target > arr[mid]) left = mid + 1;

            if (arr[mid] > target) right = mid - 1;
        }
        return -1;
    }

    public static int erfen(int[] arr, int target, int left, int right) {

        if (arr == null) {
            return -1;
        }

        if (arr[left] > target || arr[right] < target || left > right) {
            return -1;
        }
        int middle = (left + right) / 2;
        if (target < arr[middle]) {
            erfen(arr, target, left, middle - 1);
        } else if (target > arr[middle]) {
            erfen(arr, target, middle + 1, right);
        } else {
            return middle;
        }
        return -1;
    }


}
